package com.codegeek.project.slope.controller;

import com.codegeek.common.utils.poi.ExcelUtil;
import com.codegeek.framework.aspectj.lang.annotation.Log;
import com.codegeek.framework.aspectj.lang.enums.BusinessType;
import com.codegeek.framework.web.controller.BaseController;
import com.codegeek.framework.web.domain.AjaxResult;
import com.codegeek.framework.web.page.TableDataInfo;
import com.codegeek.project.slope.domain.*;
import com.codegeek.project.slope.service.ISlopeRepairRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * 设备维修记录Controller
 *
 * @author codegeek
 * @date 2020-02-17
 */
@RestController
@RequestMapping("/slope/record")
@Api(value="设备维修记录Controller",tags={"设备维修记录操作接口"})
public class SlopeRepairRecordController extends BaseController
{
    @Autowired
    private ISlopeRepairRecordService slopeRepairRecordService;

    /**
     * 查询设备维修记录列表
     */
    @ApiOperation("查询设备维修记录列表")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('slope:record:list')")
    public TableDataInfo list(SlopeRepairRecord slopeRepairRecord)
    {
        startPage();
        List<SlopeRepairRecord> list = slopeRepairRecordService.selectSlopeRepairRecordList(slopeRepairRecord);
        return getDataTable(list);
    }

    /**
     * 导出设备维修记录列表
     */
    @ApiOperation("导出设备维修记录列表")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SlopeRepairRecord slopeRepairRecord)
    {
        List<SlopeRepairRecord> list = slopeRepairRecordService.selectSlopeRepairRecordList(slopeRepairRecord);
        ExcelUtil<SlopeRepairRecord> util = new ExcelUtil<SlopeRepairRecord>(SlopeRepairRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 导出设备维修记录列表
     */
    @ApiOperation("导出设备维修记录列表")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @GetMapping("/exportBySensor")
    @PreAuthorize("@ss.hasPermi('slope:record:export')")
    public AjaxResult exportBySensor(SlopeRepairRecord slopeRepairRecord)
    {
        List<SlopeRepairRecord> list = slopeRepairRecordService.selectSlopeRepairRecordList(slopeRepairRecord);
        List<SlopeSensorRepair> repairs = new LinkedList<>();
        for(SlopeRepairRecord record : list){
            SlopeSensorRepair repair = new SlopeSensorRepair();
            SlopeSensor sensor = record.getSensor();
            SlopeDevice device = sensor.getDevice();
            SlopeSensorType sensorType = sensor.getSensorType();
            SlopePoint point = device.getPoint();
            repair.setSeq(record.getRepairRecordId());
            repair.setName(device.getDeviceName());
            repair.setSensorType(sensorType.getTypeName());
            repair.setNumber(device.getNumber());
            repair.setStartTime(record.getStartTime());
            repair.setEndTime(record.getEndTime());
            repair.setRespo(record.getRespo());
            repair.setLon(point.getLon());
            repair.setLat(point.getLat());
            repairs.add(repair);
        }
        ExcelUtil<SlopeSensorRepair> util = new ExcelUtil<>(SlopeSensorRepair.class);
        return util.exportExcel(repairs, "record");
    }

    /**
     * 获取设备维修记录详细信息
     */
    @ApiOperation("获取设备维修记录详细信息")
    @GetMapping(value = "/{repairRecordId}")
    @PreAuthorize("@ss.hasPermi('slope:record:export')")
    public AjaxResult getInfo(@PathVariable("repairRecordId") Long repairRecordId)
    {
        return AjaxResult.success(slopeRepairRecordService.selectSlopeRepairRecordById(repairRecordId));
    }

    /**
     * 新增设备维修记录
     */
    @ApiOperation("新增设备维修记录")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    @PreAuthorize("@ss.hasPermi('slope:record:add')")
    public AjaxResult add(@RequestBody SlopeRepairRecord slopeRepairRecord)
    {
        return toAjax(slopeRepairRecordService.insertSlopeRepairRecord(slopeRepairRecord));
    }

    /**
     * 修改设备维修记录
     */
    @ApiOperation("修改设备维修记录")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('slope:record:edit')")
    public AjaxResult edit(@RequestBody SlopeRepairRecord slopeRepairRecord)
    {
        return toAjax(slopeRepairRecordService.updateSlopeRepairRecord(slopeRepairRecord));
    }

//    /**
//     * 删除设备维修记录
//     */
//    @ApiOperation("删除设备维修记录")
//    @Log(title = "设备管理", businessType = BusinessType.DELETE)
//    public AjaxResult remove(@PathVariable Long[] repairRecordIds)
//    {
//        return toAjax(slopeRepairRecordService.deleteSlopeRepairRecordByIds(repairRecordIds));
//    }
}
